package com.karry.yl.service.services.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.karry.commons.base.impl.SuperServiceImpl;
import com.karry.yl.dao.services.MealMapper;
import com.karry.yl.domain.services.Meal;
import com.karry.yl.domain.services.MealItems;
import com.karry.yl.service.services.MealItemsService;
import com.karry.yl.service.services.MealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class MealServiceImpl extends SuperServiceImpl<MealMapper,Meal> implements MealService {

    @Autowired
    private MealItemsService mealItemsService;
    @Autowired
    private MealService mealService;
    @Override
    @Transactional
    public void save(Meal meal, List<MealItems> mealItemsList) {
        baseMapper.insert(meal);
        for(MealItems m : mealItemsList){
            m.setMealId(meal.getId());
            m.setMealName(meal.getName());
            mealItemsService.insert(m);
        }
    }

    @Override
    @Transactional
    public void update(Meal meal, List<MealItems> mealItemsList) {
        this.updateById(meal);
        //删除旧的子项
        mealItemsService.delete(new EntityWrapper<MealItems>().eq("mealId",meal.getId()));
        for(MealItems m : mealItemsList){
            m.setMealId(meal.getId());
            m.setMealName(meal.getName());
            mealItemsService.insert(m);
        }
    }

    @Override
    public List<Meal> selectAll(Long orgId) {
        return baseMapper.selectAll(orgId);
    }

    @Override
    public void delete(List<Long> ids) {
        mealService.deleteBatchIds(ids);
        for(long n:ids){
            mealItemsService.delete(new EntityWrapper<MealItems>().eq("mealId",n));
        }
    }
}
